package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.Member;
import com.itheima.POJO.MemberInfo;
import com.itheima.dao.PhysicalUploadMapper;
import com.itheima.dao.orderMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.PhysicalUploadService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * @date:
 * @author: majiao
 * @description:体检上传
 */
@Service(interfaceClass = PhysicalUploadService.class)
@Transactional
public class PhysicalUploadServiceImpl implements PhysicalUploadService {
    @Autowired
    private PhysicalUploadMapper physicalUploadMapper;

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private orderMapper orderDao;

    @Override
    // 体检上传:初始化查询所有
    public PageResult findPage(QueryPageBean queryPageBean) throws Exception {
        String queryString = queryPageBean.getQueryString();
        // 分页助手
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 将查询结果封装到Page的集合中,并从中取出总记录数和当前页集合,遍历集合获取到会员id
        Page<Member> page = physicalUploadMapper.findPage(queryString);
        List<Member> result = page.getResult();
        // 定义新的集合,存放添加日期和套餐名称后的member对象
        List<Member> newResult = new ArrayList<>();
        for (Member member : result) {
            Integer memberId = member.getId();
            // 根据会员表id和预约id表和套餐表id关联查询, 将日期和套餐名称封装到map集合中设置到member的属性中
            Map<String, Object> map = physicalUploadMapper.findField(memberId);
            Integer setmealID = physicalUploadMapper.findSetmealID(memberId);
            if (setmealID != null) {
                String setMealName = physicalUploadMapper.findSetmealNameById(setmealID);
                map.put("setMealName", setMealName);
            }
            if (map != null) {
                String physicalTime = DateUtils.parseDate2String((Date) map.get("physicalTime"));
                String name = (String) map.get("setMealName");
                member.setPhysicalTime(physicalTime);
                member.setSetMealName(name);
                newResult.add(member);
            }
        }
        return new PageResult(page.getTotal(), newResult);
    }

    @Override
    // 体检上传:删除按钮,删除报告并修改memberId为0
    public Result deleteMemberInfo(Integer id) {
        String status = physicalUploadMapper.selectReportStatusById(id);
        if ("1".equals(status)) {
            physicalUploadMapper.deleteMemberInfo(id);
            physicalUploadMapper.updateStatus0(id);
            return new Result(true, "删除会员数据文件成功");
        } else {
            return new Result(false, "该会员暂无数据文件,请先上传");
        }
    }

    @Override
    // 新增按钮:按照先后顺序,只能先将表格对象存到redis,等提交表单的时候获取缓存的表格对象.然后先插入表单数据,返回主键id绑定表格的数据提交到数据库
    public void addMember(Member member) {
        physicalUploadMapper.addMember(member);
        Integer memberId = member.getId();
        //获取redis中memberInfo数据
        String memberInfo = jedisPool.getResource().get("memberInfo");
        MemberInfo memberInfo1 = JSON.parseObject(memberInfo, MemberInfo.class);
        memberInfo1.setId(memberId);
        //插入数据库预约表中的会员id和体检时间
        physicalUploadMapper.insertRelation(memberId, member.getPhysicalTime());
        physicalUploadMapper.insertMemberInfo(memberInfo1);


    }

    @Override
    public void uploadTable(MemberInfo memberInfo) {
        //如果次数大于0则说明一天插入多条数据了则更新数据库
        //如果次数==0则可以正常添加
        if (memberInfo != null) {
            //查询数据库当天日期的次数
            Long count = physicalUploadMapper.countMemberIdInfo(memberInfo.getId());
            if (count > 0) {
                //如果次数大于0则说明数据已存在,覆盖并且修改会员状态为已添加
                physicalUploadMapper.updateMemberInfo(memberInfo);
                physicalUploadMapper.updateMemberRepotStatus(memberInfo.getId());
            } else {
//                    如果次数==0则可以正常添加,同时添加后修改状态为1已上传
                physicalUploadMapper.insertMemberInfo(memberInfo);
                physicalUploadMapper.updateMemberRepotStatus(memberInfo.getId());
            }
        }
    }

    @Override
    //数据回显,点击上传中的数据回显
    public Result findMemberById(Integer id) {
        try {
            Member member = physicalUploadMapper.findMemberById(id);
            //根据会员id查询预约时间
            String physicalTime = DateUtils.parseDate2String(physicalUploadMapper.findOrderDate(id));
            member.setPhysicalTime(physicalTime);
            jedisPool.getResource().setex("memberid", 300, member.getId().toString());
            return new Result(true, "请求会员数据成功", member);
        } catch (Exception e) {
            return new Result(false, "获取数据失败,请检查当前页面");
        }
    }

}

